#include "widget.h"
#include "ui_widget.h"
#include "dataread.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget), timeMs(600)
{
    ui->setupUi(this);
    connect(ui->btnInit, &QPushButton::clicked, &initDialog, &Dialog::show);
    connect(&this->initDialog, &Dialog::initDeltaZMaxSignal, this, &Widget::initDeltaZMaxSlot);
//    connect(&this->dataread, &DATAREAD::updateDeltaZSingal, this, &Widget::updateDeltaZSlot);
    connect(&dataread, &DATAREAD::updateWarn, this, &Widget::updateWarnSlot);
    connect(&this->t, &QTimer::timeout, this, &Widget::setMs);
    menuInit();

    ui->btnBegin->setEnabled(false);

    ui->btn6->installEventFilter(this);
    ui->btn7->installEventFilter(this);
    ui->btn8->installEventFilter(this);
    ui->btn9->installEventFilter(this);
    ui->btn10->installEventFilter(this);
    ui->btn11->installEventFilter(this);
    ui->btn12->installEventFilter(this);
    ui->btn13->installEventFilter(this);
    ui->btn14->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initDeltaZMaxSlot(QVector<double> vec)
{
    dataread.initDeltaZMax(vec);
    ui->btnBegin->setEnabled(true);
}

void Widget::on_btnBegin_clicked()
{
    dataread.openFile();
    updateDeltaZSlot(dataread.delta_z_vec);
//    updataTable();
    t.start(1000);
}

void Widget::updateDeltaZSlot(QVector<double> vec)
{
    qDebug() << vec.size();
    ui->lab6->setText(QString::number(vec.at(5)));
    ui->lab7->setText(QString::number(vec.at(6)));
    ui->lab8->setText(QString::number(vec.at(7)));
    ui->lab9->setText(QString::number(vec.at(8)));
    ui->lab10->setText(QString::number(vec.at(9)));
    ui->lab11->setText(QString::number(vec.at(10)));
    ui->lab12->setText(QString::number(vec.at(11)));
    ui->lab13->setText(QString::number(vec.at(12)));
    ui->lab14->setText(QString::number(vec.at(13)));
}

void Widget::on_btn14_clicked()
{
    d14.setID(14);
    d14.clearTable();
    d14.setData(dataread.datamap, dataread.delta_z_max_vec.at(13));
    d14.show();

}

void Widget::on_btn13_clicked()
{
    d13.setID(13);
    d13.clearTable();
    d13.setData(dataread.datamap, dataread.delta_z_max_vec.at(12));
    d13.show();
}

void Widget::on_btn12_clicked()
{
    d12.setID(12);
    d12.clearTable();
    d12.setData(dataread.datamap, dataread.delta_z_max_vec.at(11));
    d12.show();
}

void Widget::on_btn11_clicked()
{
    d11.setID(11);
    d11.clearTable();
    d11.setData(dataread.datamap, dataread.delta_z_max_vec.at(10));
    d11.show();
}

void Widget::on_btn10_clicked()
{
    d10.setID(10);
    d10.clearTable();
    d10.setData(dataread.datamap, dataread.delta_z_max_vec.at(9));
    d10.show();
}

void Widget::on_btn9_clicked()
{
    d9.setID(9);
    d9.clearTable();
    d9.setData(dataread.datamap, dataread.delta_z_max_vec.at(8));
    d9.show();
}

void Widget::on_btn8_clicked()
{
    d8.setID(8);
    d8.clearTable();
    d8.setData(dataread.datamap, dataread.delta_z_max_vec.at(7));
    d8.show();
}

void Widget::on_btn7_clicked()
{
    d7.setID(7);
    d7.clearTable();
    d7.setData(dataread.datamap, dataread.delta_z_max_vec.at(6));
    d7.show();
}

void Widget::on_btn6_clicked()
{

    d6.setID(6);
    d6.clearTable();
    d6.setData(dataread.datamap, dataread.delta_z_max_vec.at(5));
    d6.show();

}

void Widget::on_btn5_clicked()
{
    //setDeltaZMax(5);
}

void Widget::on_btn4_clicked()
{
    //setDeltaZMax(4);
}

void Widget::on_btn3_clicked()
{
    //setDeltaZMax(3);
}

void Widget::on_btn2_clicked()
{
    //setDeltaZMax(2);
}

void Widget::on_btn1_clicked()
{
    //setDeltaZMax(1);

}

void Widget::updateWarnSlot(int index, bool iswarn)
{
    switch (index) {
    case 5:
        if(iswarn)
            ui->btn6->setStyleSheet("background-color:red");
        else
            ui->btn6->setStyleSheet("background-color:white");
        break;
    case 6:
        if(iswarn)
            ui->btn7->setStyleSheet("background-color:red");
        else
            ui->btn7->setStyleSheet("background-color:white");
        break;
    case 7:
        if(iswarn)
            ui->btn8->setStyleSheet("background-color:red");
        else
            ui->btn8->setStyleSheet("background-color:white");
        break;
    case 8:
        if(iswarn)
            ui->btn9->setStyleSheet("background-color:red");
        else
            ui->btn9->setStyleSheet("background-color:white");
        break;
    case 9:
        if(iswarn)
            ui->btn10->setStyleSheet("background-color:red");
        else
            ui->btn10->setStyleSheet("background-color:white");
        break;
    case 10:
        if(iswarn)
            ui->btn11->setStyleSheet("background-color:red");
        else
            ui->btn11->setStyleSheet("background-color:white");
        break;
    case 11:
        if(iswarn)
            ui->btn12->setStyleSheet("background-color:red");
        else
            ui->btn12->setStyleSheet("background-color:white");
        break;
    case 12:
        if(iswarn)
            ui->btn13->setStyleSheet("background-color:red");
        else
            ui->btn13->setStyleSheet("background-color:white");
        break;
    case 13:
        if(iswarn)
            ui->btn14->setStyleSheet("background-color:red");
        else
            ui->btn14->setStyleSheet("background-color:white");
        break;
    }
}

bool Widget::eventFilter(QObject *watch, QEvent *event)
{
    if(watch==ui->btn6)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(6);
            qDebug() << "popMenu6";
            popMenu6->clear();
            popMenu6->addAction(unWarn6);
            popMenu6->addAction(setMax6);
            popMenu6->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn7)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(7);
            popMenu7->clear();
            popMenu7->addAction(unWarn7);
            popMenu7->addAction(setMax7);
            popMenu7->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn8)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(8);
            popMenu8->clear();
            popMenu8->addAction(unWarn8);
            popMenu8->addAction(setMax8);
            popMenu8->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn9)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(9);
            popMenu9->clear();
            popMenu9->addAction(unWarn9);
            popMenu9->addAction(setMax9);
            popMenu9->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn10)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(10);
            popMenu10->clear();
            popMenu10->addAction(unWarn10);
            popMenu10->addAction(setMax10);
            popMenu10->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn11)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(11);
            popMenu11->clear();
            popMenu11->addAction(unWarn11);
            popMenu11->addAction(setMax11);
            popMenu11->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn12)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(12);
            popMenu12->clear();
            popMenu12->addAction(unWarn12);
            popMenu12->addAction(setMax12);
            popMenu12->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn13)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(13);
            popMenu13->clear();
            popMenu13->addAction(unWarn13);
            popMenu13->addAction(setMax13);
            popMenu13->exec(QCursor::pos());
        }
    }
    if(watch==ui->btn14)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::RightButton)
        {
            //setDeltaZMax(14);
            popMenu14->clear();
            popMenu14->addAction(unWarn14);
            popMenu14->addAction(setMax14);
            popMenu14->exec(QCursor::pos());
        }
    }

    return QWidget::eventFilter(watch, event);
}

void Widget::setDeltaZMax(int index)
{
    qDebug() << "index" << index;
    bool ok;
    double res = QInputDialog::getDouble(nullptr, "set max delta z", "max delta z", 0, -2147483647, 2147483647, 5, &ok);
    if(ok)
    {
        qDebug() <<res;
        dataread.setDeltaZMax(index - 1, res);
    }
    //dataread.checkwarn();
}

void Widget::menuInit()
{
    popMenu6 = new QMenu(this);
    unWarn6 = new QAction(this);
    setMax6 = new QAction(this);
    unWarn6->setText("取消警告");
    setMax6->setText("设置MaxZ");
    connect(unWarn6, &QAction::triggered, this, &Widget::SetUnwarn6);
    connect(setMax6, &QAction::triggered, this, &Widget::setDeltaZ6);

    popMenu7 = new QMenu(this);
    unWarn7 = new QAction(this);
    setMax7 = new QAction(this);
    unWarn7->setText("取消警告");
    setMax7->setText("设置MaxZ");
    connect(unWarn7, &QAction::triggered, this, &Widget::SetUnwarn7);
    connect(setMax7, &QAction::triggered, this, &Widget::setDeltaZ7);

    popMenu8 = new QMenu(this);
    unWarn8 = new QAction(this);
    setMax8 = new QAction(this);
    unWarn8->setText("取消警告");
    setMax8->setText("设置MaxZ");
    connect(unWarn8, &QAction::triggered, this, &Widget::SetUnwarn8);
    connect(setMax8, &QAction::triggered, this, &Widget::setDeltaZ8);

    popMenu9 = new QMenu(this);
    unWarn9 = new QAction(this);
    setMax9 = new QAction(this);
    unWarn9->setText("取消警告");
    setMax9->setText("设置MaxZ");
    connect(unWarn9, &QAction::triggered, this, &Widget::SetUnwarn9);
    connect(setMax9, &QAction::triggered, this, &Widget::setDeltaZ9);

    popMenu10 = new QMenu(this);
    unWarn10 = new QAction(this);
    setMax10 = new QAction(this);
    unWarn10->setText("取消警告");
    setMax10->setText("设置MaxZ");
    connect(unWarn10, &QAction::triggered, this, &Widget::SetUnwarn10);
    connect(setMax10, &QAction::triggered, this, &Widget::setDeltaZ10);

    popMenu11 = new QMenu(this);
    unWarn11 = new QAction(this);
    setMax11 = new QAction(this);
    unWarn11->setText("取消警告");
    setMax11->setText("设置MaxZ");
    connect(unWarn11, &QAction::triggered, this, &Widget::SetUnwarn11);
    connect(setMax11, &QAction::triggered, this, &Widget::setDeltaZ11);

    popMenu12 = new QMenu(this);
    unWarn12 = new QAction(this);
    setMax12 = new QAction(this);
    unWarn12->setText("取消警告");
    setMax12->setText("设置MaxZ");
    connect(unWarn12, &QAction::triggered, this, &Widget::SetUnwarn12);
    connect(setMax12, &QAction::triggered, this, &Widget::setDeltaZ12);

    popMenu13 = new QMenu(this);
    unWarn13 = new QAction(this);
    setMax13 = new QAction(this);
    unWarn13->setText("取消警告");
    setMax13->setText("设置MaxZ");
    connect(unWarn13, &QAction::triggered, this, &Widget::SetUnwarn13);
    connect(setMax13, &QAction::triggered, this, &Widget::setDeltaZ13);

    popMenu14 = new QMenu(this);
    unWarn14 = new QAction(this);
    setMax14 = new QAction(this);
    unWarn14->setText("取消警告");
    setMax14->setText("设置MaxZ");
    connect(unWarn14, &QAction::triggered, this, &Widget::SetUnwarn14);
    connect(setMax14, &QAction::triggered, this, &Widget::setDeltaZ14);

}

void Widget::setMs()
{
    if(timeMs >=1)
    {
        timeMs--;
        t.start(1000);
    }
    else
    {
        timeMs = 600;
        on_btnBegin_clicked();
        t.start();
    }
}

void Widget::setDeltaZ6()
{
    setDeltaZMax(6);
}

void Widget::SetUnwarn6()
{
    qDebug() << "SetUnwarn6";
    ui->btn6->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ7()
{
    setDeltaZMax(7);

}

void Widget::SetUnwarn7()
{
    ui->btn7->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ8()
{
    setDeltaZMax(8);

}

void Widget::SetUnwarn8()
{
    ui->btn8->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ9()
{
    setDeltaZMax(9);
    ui->btn9->setStyleSheet("background-color:white");
}

void Widget::SetUnwarn9()
{
    ui->btn9->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ10()
{
    setDeltaZMax(10);
    ui->btn10->setStyleSheet("background-color:white");
}

void Widget::SetUnwarn10()
{
    ui->btn10->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ11()
{
    setDeltaZMax(11);

}

void Widget::SetUnwarn11()
{
    ui->btn11->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ12()
{
    setDeltaZMax(12);

}

void Widget::SetUnwarn12()
{
    ui->btn12->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ13()
{
    setDeltaZMax(13);

}

void Widget::SetUnwarn13()
{
    ui->btn13->setStyleSheet("background-color:white");
}

void Widget::setDeltaZ14()
{
    setDeltaZMax(14);

}

void Widget::SetUnwarn14()
{
    ui->btn14->setStyleSheet("background-color:white");
}
/*
 1 2 3
 4 5 6
 7 8 9
 */
